package com.hundred.home.module.screen;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.util.StringUtil;
import com.hundred.comment.manager.CommentManager;
import com.hundred.item.dao.dataobject.AppItemDO;
import com.hundred.item.manager.AppItemManager;
import com.hundred.user.dao.dataobject.AppUserDO;
import com.hundred.user.manager.UserManager;

public class AbtainPoints {


	private static final Logger logger = LoggerFactory.getLogger(AbtainPoints.class);

	@Autowired
    private HttpServletRequest request;
	@Autowired
	private AppItemManager itemManager;
	@Autowired
	private CommentManager commentManager;
	@Autowired
	private UserManager userManager;
	
	public void execute(Context context, Navigator nav) {
		
		String pointStr = request.getParameter("point_num");
		String taskIdStr = request.getParameter("task_id");
		String itemIdStr = request.getParameter("item_id");
		int points = 0;
		if(!StringUtil.isBlank(pointStr)){
			points=Integer.parseInt(pointStr);
		}
		int taskId = 0;
		if(!StringUtil.isBlank(taskIdStr)){
			taskId=Integer.parseInt(taskIdStr);
		}

		if(taskId == 0){
			return;
		}
		int itemId = 0;
		if(!StringUtil.isBlank(itemIdStr)){
			itemId=Integer.parseInt(itemIdStr);
		}
		
		String userIdStr = request.getParameter("userid");
		logger.error(userIdStr);
		if(userIdStr!=null){
			AppUserDO updateUserDO = userManager.getAppUserByUserId(userIdStr);
			if(updateUserDO==null){
				logger.error("user is null! check the get method!");
				return;
			}
			constructPointsStamp(taskId,updateUserDO);
			AppItemDO appItemDO = itemManager.getAppItemById(itemId);
			if(taskId==1){
				appItemDO.setLikeNum(appItemDO.getLikeNum()+1);
				itemManager.updateAppItemInfo(appItemDO);
			}
			if(taskId==2){
				appItemDO.setCollectNum(appItemDO.getCollectNum()+1);
				itemManager.updateAppItemInfo(appItemDO);
			}
			context.put("userDO", updateUserDO);
		}
		
	}

	private  String getTimeStamp(){
		StringBuffer sb = new StringBuffer();
		Calendar c = Calendar.getInstance(); 
		sb.append(c.get(Calendar.DAY_OF_YEAR));
		return sb.toString();
	}
	
	private String constructPointsStamp(int stampId,AppUserDO updateUserDO){
		String originalStamp = updateUserDO.getLastLoginIp();
		if(originalStamp==null || originalStamp.length()<1){
			return "a:1_0;b:1_0;c:1_0;d:1_0;e:1_0";
		}
		Map<String,Integer> limitMap = new HashMap<String,Integer>();
		String[] pointArray = originalStamp.split(";");
		for(String one:pointArray){
			String[] singlePoint = one.split("_");
			limitMap.put(singlePoint[0], Integer.parseInt(singlePoint[1]));
		}
		String needRemove = "";
		switch(stampId){
			case 1:
				if(limitMap.get("b:"+getTimeStamp())!=null){
					int count = limitMap.get("b:"+getTimeStamp());
					count+=1;
					limitMap.put("b:"+getTimeStamp(), count);
				}
				else{
					for(String key:limitMap.keySet()){
						if(key.startsWith("b")){
							needRemove= key;
						}
					}
					limitMap.remove(needRemove);
					limitMap.put("b:"+getTimeStamp(), 1);
				}
				updateUserDO.setPoints(updateUserDO.getPoints()+10);
				break;
			case 2:
				if(limitMap.get("b:"+getTimeStamp())!=null){
					int count = limitMap.get("b:"+getTimeStamp());
					count+=1;
					limitMap.put("b:"+getTimeStamp(), count);
				}
				else{
					for(String key:limitMap.keySet()){
						if(key.startsWith("b")){
							needRemove= key;
						}
					}
					limitMap.remove(needRemove);
					limitMap.put("b:"+getTimeStamp(), 1);
				}
				break;
			case 3:
				if(limitMap.get("c:"+getTimeStamp())!=null){
					int count = limitMap.get("c:"+getTimeStamp());
					count+=1;
					limitMap.put("c:"+getTimeStamp(), count);
				}
				else{
					for(String key:limitMap.keySet()){
						if(key.startsWith("c")){
							needRemove= key;
						}
					}
					limitMap.remove(needRemove);
					limitMap.put("c:"+getTimeStamp(), 1);
				}
				break;
			case 4:
				if(limitMap.get("d:"+getTimeStamp())!=null){
					int count = limitMap.get("d:"+getTimeStamp());
					count+=1;
					limitMap.put("d:"+getTimeStamp(), count);
				}
				else{
					for(String key:limitMap.keySet()){
						if(key.startsWith("d")){
							needRemove= key;
						}
					}
					limitMap.remove(needRemove);
					limitMap.put("d:"+getTimeStamp(), 1);
				}
				break;
			case 5:
				if(limitMap.get("e:"+getTimeStamp())!=null){
					int count = limitMap.get("e:"+getTimeStamp());
					count+=1;
					limitMap.put("e:"+getTimeStamp(), count);
				}
				else{
					for(String key:limitMap.keySet()){
						if(key.startsWith("e")){
							needRemove= key;
						}
					}
					limitMap.remove(needRemove);
					limitMap.put("e:"+getTimeStamp(), 1);
				}
				break;
			default: ;
		}
		String resultFinalStr = convertMap2String(limitMap);
		updateUserDO.setLastLoginIp(resultFinalStr);
		userManager.updateUserInfo(updateUserDO);
		return resultFinalStr;
	}
	
	private String convertMap2String(Map<String,Integer> limitMap ){
		
		StringBuffer sb = new StringBuffer();
		
		if(limitMap!=null && limitMap.size()>0){
			int m = limitMap.size();
			for(String key:limitMap.keySet()){
				sb.append(key);
				sb.append("_");
				sb.append(limitMap.get(key));
				if(m>1){
					sb.append(";");
				}
				m--;
			}
		}
		
		return sb.toString();
	}

	public AppItemManager getItemManager() {
		return itemManager;
	}


	public void setItemManager(AppItemManager itemManager) {
		this.itemManager = itemManager;
	}


	public CommentManager getCommentManager() {
		return commentManager;
	}


	public void setCommentManager(CommentManager commentManager) {
		this.commentManager = commentManager;
	}


	public UserManager getUserManager() {
		return userManager;
	}


	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

}
